package com.unclexi.factory.simplefactory;

import com.unclexi.factory.AliWork;
import com.unclexi.factory.GoogleWork;
import com.unclexi.factory.IWork;

/**
 * @Description: This Class is About ...
 *
 * @author Uncle.Xi [A Handsome Man]
 * @Date 2019/3/10 0:31
 * @Version V.0.0.1
 * @Environment JDK1.8.0_191
 */
public class LaGouFactory {

    /**
     * 最原始的工厂
     * @param name
     * @return
     */
    public IWork create0(String name){
        if("ali".equals(name)){
            return new AliWork();
        }else if("google".equals(name)){
            return new GoogleWork();
        }else {
            return null;
        }
    }

    /**
     * 稍微灵活
     * @param className
     * @return
     */
    public IWork create1(String className){
        try {
            if (!(null == className || "".equals(className))) {
                return (IWork) Class.forName(className).newInstance();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户需要知道自己要什么，非傻瓜式
     * @param clazz
     * @return
     */
    public IWork create(Class<? extends IWork> clazz){
        try {
            if (null != clazz) {
                return clazz.newInstance();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
